cmake_minimum_required(VERSION 3.16)

project(bamboo-support-msvc VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


# --------------------------
# 指定源文件字符集为 UTF-8
# --------------------------

# 对于 MSVC 编译器（Visual Studio）
if(MSVC)
    # 设置源字符集为 UTF-8（/utf-8 选项）
    add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/utf-8>")
    add_compile_options("$<$<COMPILE_LANGUAGE:C>:/utf-8>")

    # 可选：禁用 BOM 检查（避免部分文件因无BOM报错）
    add_compile_options(/wd4828)
endif()


set(INC_DIR ${CMAKE_SOURCE_DIR}/include)
set(LIB_DIR ${CMAKE_SOURCE_DIR}/lib)


#set(CURL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include/curl)
#set(CURL_LIBRARY_DIR ${CMAKE_SOURCE_DIR}/lib/curl)
#include_directories(${CURL_INCLUDE_DIR})
#link_directories(${CURL_LIBRARY_DIR})


include_directories(${INC_DIR})
link_directories(${LIB_DIR})

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Sql Charts LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Sql Charts LinguistTools)

set(TS_FILES lang/bamboo-support-msvc_zh_CN.ts)

set(PROJECT_SOURCES
        main.cpp
        resources/bootstrap/mainwindow.cpp
        resources/bootstrap/mainwindow.h
        resources/bootstrap/mainwindow.ui
        ${TS_FILES}
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(bamboo-support-msvc
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
        include/chinactprealtime.h include/chinactprealtime.cpp
        include/chinactptrader.h include/chinactptrader.cpp

        resources/quotation/quotationform.h resources/quotation/quotationform.cpp resources/quotation/quotationform.ui
        resources/help/aboutdialog.h resources/help/aboutdialog.cpp resources/help/aboutdialog.ui
        plugin/klinewidget.h plugin/klinewidget.cpp
        resources/strategy/strategy.h resources/strategy/strategy.cpp resources/strategy/strategy.ui
        resources/position/position.h resources/position/position.cpp resources/position/position.ui
        resources/position/orderdialog.h resources/position/orderdialog.cpp resources/position/orderdialog.ui
        resources/finance/finance.h resources/finance/finance.cpp resources/finance/finance.ui
        resources/common/kchartform.h resources/common/kchartform.cpp resources/common/kchartform.ui
        include/bgridwidget.h include/bgridwidget.cpp
        include/bthread.h include/bthread.cpp
        include/ssh/bamboossh.h include/ssh/bamboossh.cpp

        plugin/db.h plugin/db.cpp
        plugin/dao.h plugin/dao.cpp
        plugin/flowlayout.h plugin/flowlayout.cpp
        plugin/threadpool.h plugin/threadpool.cpp
        #plugin/httphelper.h plugin/httphelper.cpp
        thread/orderthread.h thread/orderthread.cpp
        thread/chinactpquotethread.h thread/chinactpquotethread.cpp
        plugin/quant/ema.h plugin/quant/ema.cpp
        plugin/quant/macd.h plugin/quant/macd.cpp
        plugin/mt4chart.h plugin/mt4chart.cpp
        thread/chinactptraderthread.h thread/chinactptraderthread.cpp
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET bamboo-support-msvc APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation

    qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else()
    if(ANDROID)
        add_library(bamboo-support-msvc SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(bamboo-support-msvc
            ${PROJECT_SOURCES}
        )
    endif()

    qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif()

target_link_libraries(bamboo-support-msvc PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Sql Qt${QT_VERSION_MAJOR}::Charts )
target_link_libraries(bamboo-support-msvc PRIVATE
    ${LIB_DIR}/chinactp/thostmduserapi_se.lib ${LIB_DIR}/chinactp/thosttraderapi_se.lib
    ${LIB_DIR}/libssh2/libssh2.lib ${LIB_DIR}/libssh2/libssh2_static.lib
    ${LIB_DIR}/ws2/WS2_32.lib
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.bamboo-support-msvc)
endif()
set_target_properties(bamboo-support-msvc PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS bamboo-support-msvc
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(bamboo-support-msvc)
endif()
